package com.honraepoder.strateegia;

import java.util.Random;

import com.honraepoder.strateegia.R;
import com.honraepoder.strateegia.Rects.Valor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class GameView extends View implements Runnable {

	private final static int INTERVAL = 10;
	private boolean running = true;
	private Rects[][] rects;
	private Paint paint;
	private Paint paint2;
	int cont = 0;
	int temp = 0;
	private Bitmap[] numeros = new Bitmap[16];
	private Bitmap[] img;
	int ind = 0;
	int num;
	int Height;
	int Width;
	int cont2;
	int cont3;
	private Rects fundo;
	private Rects fundo2;
	private Paint paint3;
	private Rects rect16;
	private Bitmap img16;
	private Rects rectR;
	private Bitmap imgR;
	
	public GameView(Context context) {
		super(context);

		paint = new Paint();
		paint2 = new Paint();
		paint3 = new Paint();
		
		fundo = new Rects();
		fundo2= new Rects();
		
		rect16 = new Rects();
		img16 = BitmapFactory.decodeResource(getResources(),R.drawable.n16);
		
		rectR = new Rects();
		imgR = BitmapFactory.decodeResource(getResources(),R.drawable.btr);
		
		img = new Bitmap[15];

		numeros[0] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n1);
		numeros[1] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n2);
		numeros[2] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n3);
		numeros[3] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n4);
		numeros[4] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n5);
		numeros[5] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n6);
		numeros[6] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n7);
		numeros[7] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n8);
		numeros[8] = BitmapFactory
				.decodeResource(getResources(), R.drawable.n9);
		numeros[9] = BitmapFactory.decodeResource(getResources(),
				R.drawable.n10);
		numeros[10] = BitmapFactory.decodeResource(getResources(),
				R.drawable.n11);
		numeros[11] = BitmapFactory.decodeResource(getResources(),
				R.drawable.n12);
		numeros[12] = BitmapFactory.decodeResource(getResources(),
				R.drawable.n13);
		numeros[13] = BitmapFactory.decodeResource(getResources(),
				R.drawable.n14);
		numeros[14] = BitmapFactory.decodeResource(getResources(),
				R.drawable.n15);
		//numeros[15] = BitmapFactory.decodeResource(getResources(),
			//	R.drawable.n16);
		rects = new Rects[6][6];

		for (int y = 0; y < rects.length; y++) {
			for (int x = 0; x < rects.length; x++) {
				rects[x][y] = new Rects();
				if (x == 3 && y == 4) {
					rects[x][y].valor = Rects.Valor.Vazio;
				} else {
					rects[x][y].valor = Rects.Valor.Cheio;
				}

			}
		}

		Thread minhaThread = new Thread(this);
		minhaThread.setPriority(Thread.MIN_PRIORITY);
		minhaThread.start();

	}

	public void run() {
		while (running) {
			try {
				Thread.sleep(INTERVAL);
			} catch (Exception e) {
				Log.e("Jogo", "Sleep da Thread");
			}

			postInvalidate();
			update();
		}
	}

	private void update() {

		if (CondicaoVitoria()) {
			//Log.e("Vitoria", "eeeee");
			//rect16.position((Height - rect16.getHeight()), Height/6);
			rect16.position(rects[4][4].getX(), rects[4][4].getY());
		
		}

		while (num < 10000) {
			num++;
			Embaralhar();
		}
		

		if (cont2 == 0) {
			Height = getHeight();
			Width = getWidth();
			if (Height != 0 && Width != 0) {
				for (int y = 0; y < rects.length; y++) {
					for (int x = 0; x < rects.length; x++) {
						rects[x][y].setHeight(Height / 6);
						rects[x][y].setWidth(Height / 6);
					}
				}
				
				fundo.setHeight(Height*4/6);
				fundo.setWidth(Height*4/6);
				
				fundo2.setHeight(Height);
				fundo2.setWidth(Width);
				
				rect16.setHeight(Height / 6);
				rect16.setWidth(Height / 6);
				
				rectR.setHeight(Width / 6);
				rectR.setWidth(Height / 3);
				
				cont2++;
			}
		}

	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);

		paint.setColor(Color.GRAY);
		paint2.setColor(Color.WHITE);
		paint3.setColor(Color.rgb(0, 0, 0));
		
		
		if(cont3==0)
		{
			cont3++;
			
			fundo2.draw(canvas, paint3);
			fundo2.position(0,0);
			
		}
		if (cont3 == 1) {
			fundo.draw(canvas, paint);
			fundo.position(Height / 6 / 2
					/ 2, Height / 6);
			cont3++;
		} 
		if (cont3 ==2) {
			cont3=0; 
			for (int y = 1; y < rects.length - 1; y++) {

				for (int x = 1; x < rects.length - 1; x++) {

					rects[x][y].position((x - 1) * Height / 6 + Height / 6 / 2
							/ 2, (y - 1) * Height / 6 + Height / 6);

					if (rects[x][y].valor == Valor.Cheio) {

						rects[x][y].draw(canvas, paint);
						canvas.drawBitmap(numeros[rects[x][y].getIndex()],
								null, rects[x][y].getR(), paint);
						if (temp < 15) {
							rects[x][y].setIndex(temp);
							temp++;
						}

					} else if (rects[x][y].valor == Valor.Vazio) {
						rects[x][y].draw(canvas, paint3);

					}
					
				}

			}
			rect16.draw(canvas, paint);
			if(CondicaoVitoria() == false){
			rect16.position(Height + rect16.getHeight(), Height/6);}
			canvas.drawBitmap(img16, null, rect16.getR(), paint);
			
			rectR.draw(canvas, paint);
			rectR.position(rects[2][4].getX(), rects[2][4].getY()+ Width/10 + Width/6);
			canvas.drawBitmap(imgR, null, rectR.getR(), paint);
			
		
		}
		
		
		

	}

	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();

		int x = (int) event.getX();
		int y = (int) event.getY();

		if (action == MotionEvent.ACTION_DOWN) {
			for (int k2 = 1; k2 < rects.length - 1; k2++) {
				for (int k = 1; k < rects.length - 1; k++) {
					if (rects[k][k2].colide(x, y)) {

						String id = Integer.toString(rects[k][k2].getIndex());
						Log.e("Index", id);
						Movimento(k, k2);

					}
				}
			}
			
			if(rectR.colide(x,y))
			{
				Recomecar();
			}

		} else if (action == MotionEvent.ACTION_UP) {
			postInvalidate();

		} else if (action == MotionEvent.ACTION_MOVE) {
			postInvalidate();
		}

		return super.onTouchEvent(event);
	}

	private void Recomecar() {
		
		num = 0;
		while (num < 10000) {
			num++;
			Embaralhar();
		}
		rect16.position(Height + rect16.getHeight(), Height/6);
	}

	private boolean CondicaoVitoria() {
		int contador = 0;
		for (int y = 1; y < rects.length - 1; y++) {

			for (int x = 1; x < rects.length - 1; x++) {

				if (rects[x][y].index == contador) {
					contador++;
					if (contador == 15) {
						return true;
					}
				} else {
					contador = 0;
					return false;
				}

			}
		}

		return false;

	}

	private void Movimento(int x, int y) {
		if (rects[x + 1][y].valor == Valor.Vazio) {

			rects[x + 1][y].valor = Valor.Cheio;
			rects[x + 1][y].setIndex(rects[x][y].getIndex());
			rects[x][y].valor = Valor.Vazio;
			rects[x][y].setIndex(rects[x + 1][y].getIndex());
		}

		if (rects[x - 1][y].valor == Valor.Vazio) {

			rects[x - 1][y].valor = Valor.Cheio;
			rects[x - 1][y].setIndex(rects[x][y].getIndex());
			rects[x][y].valor = Valor.Vazio;
			rects[x][y].setIndex(rects[x - 1][y].getIndex());

		}

		if (rects[x][y + 1].valor == Valor.Vazio) {

			rects[x][y + 1].valor = Valor.Cheio;
			rects[x][y + 1].setIndex(rects[x][y].getIndex());
			rects[x][y].valor = Valor.Vazio;
			rects[x][y].setIndex(rects[x][y + 1].getIndex());

		}

		if (rects[x][y - 1].valor == Valor.Vazio) {

			rects[x][y - 1].valor = Valor.Cheio;
			rects[x][y - 1].setIndex(rects[x][y].getIndex());
			rects[x][y].valor = Valor.Vazio;
			rects[x][y].setIndex(rects[x][y - 1].getIndex());

		}
	}

	private void Embaralhar() {
		Random b = new Random();
		int a;

		for (int y = 1; y < rects.length - 1; y++) {
			for (int x = 1; x < rects.length - 1; x++) {

				a = b.nextInt(4);

				if (a == 0) {
					if (rects[x + 1][y].valor == Valor.Vazio) {

						rects[x + 1][y].valor = Valor.Cheio;
						rects[x + 1][y].setIndex(rects[x][y].getIndex());
						rects[x][y].valor = Valor.Vazio;
						rects[x][y].setIndex(rects[x + 1][y].getIndex());
					}
				}

				if (a == 1) {
					if (rects[x - 1][y].valor == Valor.Vazio) {

						rects[x - 1][y].valor = Valor.Cheio;
						rects[x - 1][y].setIndex(rects[x][y].getIndex());
						rects[x][y].valor = Valor.Vazio;
						rects[x][y].setIndex(rects[x - 1][y].getIndex());

					}
				}
				if (a == 2) {
					if (rects[x][y + 1].valor == Valor.Vazio) {

						rects[x][y + 1].valor = Valor.Cheio;
						rects[x][y + 1].setIndex(rects[x][y].getIndex());
						rects[x][y].valor = Valor.Vazio;
						rects[x][y].setIndex(rects[x][y + 1].getIndex());

					}
				}

				if (a == 3) {
					if (rects[x][y - 1].valor == Valor.Vazio) {

						rects[x][y - 1].valor = Valor.Cheio;
						rects[x][y - 1].setIndex(rects[x][y].getIndex());
						rects[x][y].valor = Valor.Vazio;
						rects[x][y].setIndex(rects[x][y - 1].getIndex());

					}
				}
			}
		}

	}

}
